package com.yc.cloud.security.utils;

import com.yc.cloud.security.dto.MemberUserDetails;
import lombok.val;
import org.springframework.security.core.context.SecurityContextHolder;


public class MemberUtils {

    private static MemberUserDetails getUserDetail() {
        val authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            return null;
        }
        return (MemberUserDetails) authentication.getPrincipal();
    }

    public static String getCurrentUserName() {
        return getUserDetail().getUsername();
    }

    public static Long getCurrentUseId() {
        val userDetail = getUserDetail();
        if (userDetail == null) {
            return 0L;
        }
        return userDetail.getMemberAccount().getId();
    }

    public static Long getCurrentCompanyId() {
        return getUserDetail().getMemberAccount().getCompanyId();
    }

}
